package com.bsy.videostation.viewmodel

import androidx.lifecycle.MutableLiveData
import com.bsy.common.viewmodel.BaseViewModel
import com.bsy.framework.ext.data
import com.bsy.videostation.manager.UserManager
import com.bsy.videostation.model.VideoEntity
import java.util.Calendar

/**
 * Created by Sincerity on 2024/5/22.
 * Copyright
 * 描述：历史记录
 */
class HistoryViewModel : BaseViewModel() {
    //一周内
    val historyOneWeekData = MutableLiveData<MutableList<VideoEntity>>()

    //一周后
    val historyAfterOneWeekData = MutableLiveData<MutableList<VideoEntity>>()

    /**
     * 得到历史数据
     *
     */
    fun getHistoryData() {
        // 定义一周前的 Calendar 实例
        val oneWeekAgo = Calendar.getInstance().apply {
            add(Calendar.DAY_OF_YEAR, -7)
        }
        // 获取历史列表
        val historyList = UserManager().getHistoryList()
        // 分类列表
        val oneWeekList = ArrayList<VideoEntity>()
        val moreThanOneWeekList = ArrayList<VideoEntity>()

        // 创建一个共享的 Calendar 实例
        val videoTime = Calendar.getInstance()

        // 遍历历史列表，并根据时间字段分类
        for (video in historyList) {
            // 将视频的时间戳设置给共享的 Calendar 实例
            videoTime.timeInMillis = video.time.data()

            // 检查视频时间是否在一周内
            if (videoTime.after(oneWeekAgo)) {
                oneWeekList.add(video)
            } else {
                moreThanOneWeekList.add(video)
            }
        }
        historyOneWeekData.value = oneWeekList
        historyAfterOneWeekData.value = moreThanOneWeekList
    }


}